package com.sunnyint.orderprinter.utils;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sunnyint.orderprinter.entity.OrderInfo;

import java.util.Arrays;

/**
 * 思普瑞特打印机工具类
 * @author shaojun.bai
 */
public class SprintUtil {
        // 定义接口TestDLL，继承自com.sun.jna.Library
    public interface PosSdkDLL extends Library {
        // 定义并初始化接口的静态变量
        String dll = SprintUtil.class.getResource("/").getPath().substring(1)+"POS_SDK.dll";
        PosSdkDLL INSTANCE = (PosSdkDLL) Native.loadLibrary(dll,PosSdkDLL.class);
        public int POS_Control_ReSet(int id); // 初始化打印机
        public int POS_Port_OpenA(String a, int b, boolean c, String d); // 打开端口
        public int POS_Output_PrintStringA(int id, String str); // 打印字符串
//        public int POS_Output_PrintBarcodeA(int id, int a, int b, int c, int d, String e);//打印条码
        public int POS_Output_PrintData(int id, byte[] str, int length); // 打印byte数组
        public int POS_Input_ReadPrinter(int id, byte[] readStr); //读返回值
        public int POS_Port_Close(int iPrinterID);// 关口;
        public int POS_Output_PrintFontStringA(int iPrinterID, int iFont, int iThick, int iWidth, int iHeight, int iUnderLine, String lpString);//打印格式字符串
        public int POS_Output_PrintOneDimensionalBarcodeA(int iPrinterID, int iType, int iWidth, int iHeight, int hri, String lpString);//打印条形码
        public int POS_Output_PrintTwoDimensionalBarcodeA(int iPrinterID, int iType, int parameter1, int parameter2, int parameter3, String lpString);//打印二维码
        public int POS_Output_PrintBmpDirectA(int iPrinterID, String lpString);//打印bmp位图
        public int POS_Control_AlignType(int iPrinterID, int n);//居中类型
        public int POS_Control_CutPaper(int iPrinterID, int a, int b);//切纸
    }

    public static void main(String[] args) {
        printNew(null);
    }

    /**
     * 新打印订单
     * @param orderInfo 订单信息
     */
    public static void printNew(OrderInfo orderInfo) {
        int printerID = PosSdkDLL.INSTANCE.POS_Port_OpenA("COM3:9600,N,8,1", 1000, false, null); //打开com3
        //int printerID = TestDLL.INSTANCE.POS_Port_OpenA("SP-USB1", 1002, false, null); //打开usb
        //int printerID = TestDLL.INSTANCE.POS_Port_OpenA("192.168.1.114:9100", 1003, false, null); //打开网口
        if(printerID == 0)
        {
            System.out.println("Open error");
            return;
        }
        System.out.println("Open success");

//        PosSdkDLL.INSTANCE.POS_Output_PrintStringA(printerID, "这是一个测试语句!\n\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,0,0,1,"Hi, thank you for choosing our printer, We will get your the best experience!\r\n感谢您选择我们的打印机\r\n\r\n");
        PosSdkDLL.INSTANCE.POS_Control_ReSet(printerID);

        //打印开始
//        PosSdkDLL.INSTANCE.POS_Output_PrintOneDimensionalBarcodeA(printerID, 4074, 2, 50, 4013, "No.123456");
//        PosSdkDLL.INSTANCE.POS_Output_PrintTwoDimensionalBarcodeA(printerID,4102,2,77,4,"www.test.com");

        //图标
//        String icon = SprintUtil.class.getResource("/").getPath().substring(1)+"goodwork.bmp";
//        PosSdkDLL.INSTANCE.POS_Output_PrintBmpDirectA(printerID,icon);

//        PosSdkDLL.INSTANCE.POS_Control_AlignType(printerID,1);//是否居中
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,1,0,0,"牛逼餐馆\r\n客人就餐消费单\r\n");
//        PosSdkDLL.INSTANCE.POS_Control_AlignType(printerID,0);//是否居中
//
//        //循环打印的信息
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"——————————————————————\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"酒店日期：2016-5-6                餐段：晚餐\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"单号：0012                      服务员：小小\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"菜品名称         数量       规格        小计\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"——————————————————————\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"健怡雪碧         4            瓶       40.00\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"鲟龙鱼         1.5            斤       28.00\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"——————————————————————\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"                             消费合计：68.00\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"折扣代码：                   折扣金额： 0.00\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"                             应收金额：68.00\r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"打印时间：2016-5-6   21:32:49               \r\n");
//        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"餐饮系统 www.***.com.cn             \r\n\r\n\r\n");

        //给顾客
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,1,0,0,"给顾客\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"—————————\r\n");

        //美团头
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,1,0,0,"****美团外卖****\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"*xx餐厅*\r\n");

        //下单时间
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"下单时间: "+orderInfo.getPlaceOrderTime()+"\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"************************************\r\n");

        //商品列表(1号口袋)
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"-----------1号口袋---------\r\n");

        String space = "        ";//8个空
        String enter = "\r\n";//换行

        //循环商品
        for(int i=0;i<5;i++){
            StringBuilder builder = new StringBuilder();
            builder.append("黄瓜炒香肠").append(space).append("*    ").append(1).append(space).append("5.0").append("\r\n");
            PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,builder.toString());
        }

        //其它
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"-----------其它---------\r\n");

        StringBuilder boxFee = new StringBuilder();
        boxFee.append("餐盒费").append(space).append(space).append("3.o").append(enter);
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,boxFee.toString());

        StringBuilder deliveryFee = new StringBuilder();
        deliveryFee.append("配送费").append(space).append(space).append("6.5").append(enter);
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,deliveryFee.toString());

        StringBuilder minusDelivery = new StringBuilder();
        minusDelivery.append("[减配送费").append("4.0").append("]").append(space).append(space).append("-4.0").append(enter);
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,minusDelivery.toString());

        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"****************************");

        //payinfo
        StringBuilder payInfo = new StringBuilder();
        payInfo.append("(用户在线支付)");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,payInfo.toString());
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,1,0,0,"74.5元\r\n");

        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"-------------------------\r\n");

        //收货地址
        StringBuilder address = new StringBuilder();
        address.append(orderInfo.getProvince()).append(orderInfo.getCity()).append(orderInfo.getArea()).append(enter);
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,1,0,0,address.toString());

        StringBuilder phone = new StringBuilder();
        phone.append("顾客号码:").append(orderInfo.getReceivePhone()).append(enter);

        //收货人电话
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0, phone.toString());

        //警告信息
        String warnInfo = "【若无法拨通，请通过美团外卖商家办外卖端查询最新号码】";
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0, warnInfo+"\r\n");

        //打印结束
        PosSdkDLL.INSTANCE.POS_Control_CutPaper(printerID,1,3);
        //发送检查是否打印完成指令
        byte[] command = { 0x1d, 0x28, 0x48, 0x06, 0x00, 0x30, 0x30, 0x31, 0x32, 0x33, 0x34 };
        PosSdkDLL.INSTANCE.POS_Output_PrintData(printerID, command, command.length);

        //打印完成应该接收到的返回值
        byte[] rightByte = {0x37, 0x22, 0x31, 0x32, 0x33, 0x34};

        //接收返回值
        byte[] recByte = new byte[6];
        PosSdkDLL.INSTANCE.POS_Input_ReadPrinter(printerID, recByte);

        //打印返回值
        String recStr = new String(recByte);
        System.out.println("rec value is: " + recStr);

        //比较返回值是否正确
        if(Arrays.equals(rightByte, recByte)){
            System.out.println("已经打印完成");
        }
        PosSdkDLL.INSTANCE.POS_Port_Close(printerID);
        return;
    }

    /**
     * 打印订单
     * @param orderInfo 订单信息
     */
    public static void print(OrderInfo orderInfo) {
        int printerID = PosSdkDLL.INSTANCE.POS_Port_OpenA("COM3:9600,N,8,1", 1000, false, null); //打开com3
        //int printerID = TestDLL.INSTANCE.POS_Port_OpenA("SP-USB1", 1002, false, null); //打开usb
        //int printerID = TestDLL.INSTANCE.POS_Port_OpenA("192.168.1.114:9100", 1003, false, null); //打开网口
        if(printerID == 0)
        {
            System.out.println("Open error");
            return;
        }
        System.out.println("Open success");

        //TestDLL.INSTANCE.POS_Control_ReSet(printerID);
        PosSdkDLL.INSTANCE.POS_Output_PrintStringA(printerID, "这是一个测试语句!\n\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,0,0,1,"Hi, thank you for choosing our printer, We will get your the best experience!\r\n感谢您选择我们的打印机\r\n\r\n");
        PosSdkDLL.INSTANCE.POS_Control_ReSet(printerID);
        //TestDLL.INSTANCE.POS_Output_PrintBarcodeA(printerID, 4074, 2, 50, 4013, "No.123456");
        //TestDLL.INSTANCE.POS_Output_PrintBarcodeA(printerID, 4100, 2, 3, 6, "123");

        //打印开始
        PosSdkDLL.INSTANCE.POS_Output_PrintOneDimensionalBarcodeA(printerID, 4074, 2, 50, 4013, "No.123456");
        PosSdkDLL.INSTANCE.POS_Output_PrintTwoDimensionalBarcodeA(printerID,4102,2,77,4,"www.test.com");

        String icon = SprintUtil.class.getResource("/").getPath().substring(1)+"goodwork.bmp";

        PosSdkDLL.INSTANCE.POS_Output_PrintBmpDirectA(printerID,icon);
        PosSdkDLL.INSTANCE.POS_Control_AlignType(printerID,1);//是否居中
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,1,1,0,0,"***餐馆\r\n客人就餐消费单\r\n");
        PosSdkDLL.INSTANCE.POS_Control_AlignType(printerID,0);//是否居中

        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"——————————————————————\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"酒店日期：2016-5-6                餐段：晚餐\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"单号：0012                      服务员：小小\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"菜品名称         数量       规格        小计\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"——————————————————————\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"健怡雪碧         4            瓶       40.00\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"鲟龙鱼         1.5            斤       28.00\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"——————————————————————\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"                             消费合计：68.00\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"折扣代码：                   折扣金额： 0.00\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"                             应收金额：68.00\r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"打印时间：2016-5-6   21:32:49               \r\n");
        PosSdkDLL.INSTANCE.POS_Output_PrintFontStringA(printerID,0,0,0,0,0,"餐饮系统 www.***.com.cn             \r\n\r\n\r\n");

        //打印结束
        PosSdkDLL.INSTANCE.POS_Control_CutPaper(printerID,1,3);
        //发送检查是否打印完成指令
        byte[] command = { 0x1d, 0x28, 0x48, 0x06, 0x00, 0x30, 0x30, 0x31, 0x32, 0x33, 0x34 };
        PosSdkDLL.INSTANCE.POS_Output_PrintData(printerID, command, command.length);

        //打印完成应该接收到的返回值
        byte[] rightByte = {0x37, 0x22, 0x31, 0x32, 0x33, 0x34};

        //接收返回值
        byte[] recByte = new byte[6];
        PosSdkDLL.INSTANCE.POS_Input_ReadPrinter(printerID, recByte);

        //打印返回值
        String recStr = new String(recByte);
        System.out.println("rec value is: " + recStr);

        //比较返回值是否正确
        if(Arrays.equals(rightByte, recByte)){
            System.out.println("已经打印完成");
        }
        PosSdkDLL.INSTANCE.POS_Port_Close(printerID);
        return;
    }
}
